<?php

/**
 * alltosun.com 门店控制器 index.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 刘德洪 (Liudh@alltosun.com) $
 * $Date: 20151124 $
 * $Id: index.php 197630 2016-06-16 05:01:44Z gaoa $
 */
class Action
{
    private $per_page = 20;

    private $token = '';
    private $user_id = 0;
    private $user_info = array();

    public function __construct() {
        $this->token = an_request('token', 'post');

        if ($this->token) {
            $user_info = user_helper::get_user_info_by_token($this->token);
            if ($user_info && $user_info['status'] == 1) {
                $this->user_id = $user_info['id'];
                $this->user_info = $user_info;
                $this->user_info['user_id'] = $user_info['id'];
            }
        }
    }

    /**
     * 显示列表
     */
    public function index() {

    }

    /**
     * 返回抱抱b文案
     */
    public function get_baobaob_rule_text() {
        // store, agency
        $type = an_request('type', '', 'post');
        // 1 打开包包币  0 关闭
        $is_bbcoin_enable = an_request('is_bbcoin_enable', 0, 'post');
        $result = _widget('store')->get_baobaob_rule_text($type, $is_bbcoin_enable);
        api_helper::return_data(0, array('info' => $result));
    }

    /**
     * 删除门店信息
     */
    public function del_user_store() {
        api_helper::return_data(1, '此功能已关闭');

        // 验证接口
        $owner_id = an_request('owner_id', 0, 'post');

        $check_params = array(
            // 店主名
            'owner_id' => $owner_id
        );

        if (!$owner_id) {
            api_helper::return_data(1, '店铺账号有误，请联系客服!');
        }

        $api_stat_id = api_helper::check_sign($check_params, 1);

        // 是否有绑定设备
        if (!store_helper::is_user_store_bind_device($owner_id)) {
            api_helper::return_data(1, '请先解绑所有的设备!');
        }

        // 是否有未处理完的订单
        $order_num = dealers_helper::is_unset_order_user_store($owner_id);
        if ($order_num) {
            api_helper::return_data(1, '当前门店还有未完成订单!');
        }

        $result = _widget('store')->del_user_store($this->user_id, $owner_id);
        if (!$result) {
            api_helper::return_data(1, '店铺删除失败!');
        } else {
            api_helper::return_data(0, '店铺删除成功!');
        }
    }

    /**
     * 返回分店类型接口
     */
    public function get_store_type() {
        $ret = store_config::$type_arr;
        // 屏蔽影院和私人影吧签约, 统一走影院 cinema_h5
        // 只屏蔽影院 2016-08-22 @珊珊
        // unset($ret['100']);
        api_helper::return_data(0, 'success', $ret);
    }

    /**
     * 签约门店
     */
    public function create_user_store() {
        // 验证接口
        //$store_id = Request::Get('store_id', 0);
        $name = Request::Post('name', '');
        $mobile = trim(Request::Post('mobile', ''));

        //$cover = Request::Post('cover', '');
        $title = Request::Post('title', '');
        $type = Request::Post('type', 0);

        $cert_type = Request::Post('cert_type', 0);
        $cert_idcard_front = Request::Post('cert_idcard_front', '');
        $cert_idcard_back = Request::Post('cert_idcard_back', '');
        $cert_company = Request::Post('cert_company', '');

        $check_params = array(
            // 店主名
            'name' => $name,
            // 手机号
            'mobile' => $mobile,
            // 门店名
            'title' => $title,
            //                 'type'              => $type,
            // 认证类型认证类型 0营业执照+身份证 1身份证
            'cert_type' => $cert_type,
            // 身份证正面照
            'cert_idcard_front' => $cert_idcard_front,
            // 身份证背面照
            'cert_idcard_back' => $cert_idcard_back,
            // 营业执照
            'cert_company' => $cert_company
        );
        $api_stat_id = api_helper::check_sign($check_params, 1);

        api_helper::get_user_info();

        if ($this->user_info['role_type'] == user_config::AGENCY) {
            // 如果是 合伙人 身份
            $agency_id = $this->user_id;
            $marketing_id = $this->user_id;
        } elseif ($this->user_info['role_type'] == user_config::MARKETING) {
            // 如果是 市场人员 身份
            $arr = _model('user_marketing')->read(array(
                'marketing_id' => $this->user_id
            ));
            $agency_id = $arr['agency_id'];
            $marketing_id = $this->user_id;
        } else {
            api_helper::return_data(1, '身份不对');
        }

        $agency_user = user_helper::get_user_info($agency_id);

        if (!$mobile || !$name) {
            api_helper::return_data(1, '参数不能为空');
        }

        //判断 手机号是否存在
        if (!user_helper::is_username_unique($mobile)) {
            api_helper::return_data(1, '手机号已存在');
        }

        //先创建 用户信息
        //$user_id = user_helper::create_user();
        $user_info = array(
            'user_name' => $mobile,
            'realname' => $name,
            'mobile' => $mobile,
        );

        // 测试账号标记
        if (user_helper::is_test_dealers_user($agency_id)) {
            $user_info['is_enable'] = 0;
        }

        $role_type = user_config::STORE_OWNER;
        $ret_user = user_helper::create_user($user_info, $role_type);
        if (empty($ret_user)) {
            api_helper::return_data(1, '用户创建失败');
        }

        $user_id = $ret_user['user_id'];
        $password = $ret_user['password'];

        //添加总店信息
        $insert_arr = array(
            'user_id' => $user_id,
            'agency_id' => $agency_id,
            'marketing_id' => $marketing_id,

            'cert_type' => $cert_type,
            'cert_idcard_front' => $cert_idcard_front,
            'cert_idcard_back' => $cert_idcard_back,
            'cert_company' => $cert_company,

            'sign_user_id' => $this->user_id,
            'sign_time' => datetime(),
            'add_time' => datetime(),
        );
        if (isset(store_config::$type_arr[$type])) {
            $insert_arr['type'] = $type;
        }
        if ($title) {
            $insert_arr['title'] = $title;
        }

        $id = _model('user_store')->create($insert_arr);

        $ret = array(
            'user_id' => $user_id,
            'owner_id' => $user_id,
            // 'password' => $password,
        );


        global $mc_wr;
        $mc_wr->set('store_passowrd_' . $user_id, $password, 60);

        if ($id) {
            api_helper::return_data(0, 'success', $ret);
        } else {
            api_helper::return_data(1, '创建出错了');
        }

    }

    /**
     * 更新门店(编辑总店)new
     * @author gaoa@alltosun.com
     */
    public function update_user_store() {
        // 验证接口
        $owner_id = Request::Post('owner_id', 0);
        $name = Request::Post('name', '');
        $title = Request::Post('title', '');
        $type = Request::Post('type', '');
        $cert_type = Request::Post('cert_type', 0);
        $cert_idcard_front = Request::Post('cert_idcard_front', '');
        $is_cert_idcard_front = Request::Post('is_cert_idcard_front', 0);
        $cert_idcard_back = Request::Post('cert_idcard_back', '');
        $is_cert_idcard_back = Request::Post('is_cert_idcard_back', 0);
        $cert_company = Request::Post('cert_company', '');
        $is_cert_company = Request::Post('is_cert_company', 0);

        $check_params = array(
            // 总店用户id
            'owner_id' => $owner_id,
            // 店主名
            'name' => $name,
            // 门店名
            'title' => $title,
            // 门店类型
            'type' => $type,
            // 认证类型认证类型 0营业执照+身份证 1身份证
            'cert_type' => $cert_type,
            // 身份证正面照
            'cert_idcard_front' => $cert_idcard_front,
            // 是否上传正面照
            'is_cert_idcard_front' => $is_cert_idcard_front,
            // 身份证背面照
            'cert_idcard_back' => $cert_idcard_back,
            // 是否上传背面照
            'is_cert_idcard_back' => $is_cert_idcard_back,
            // 营业执照
            'cert_company' => $cert_company,
            // 是否上传营业执照
            'is_cert_company' => $is_cert_company
        );
        $api_stat_id = api_helper::check_sign($check_params, 1);

        $res = _widget('store')->update_user_store($this->user_id, $owner_id, $name, $title, $type, $cert_type, $cert_idcard_front, $is_cert_idcard_front, $cert_idcard_back, $is_cert_idcard_back, $cert_company, $is_cert_company);

        if ($res['flag']) {
            api_helper::return_data(0, $res['info']);
        } else {
            api_helper::return_data(1, $res['info']);
        }
    }

    /**
     * 查看总店信息
     * @author gaoa@alltosun.com
     */
    //     public function show_user_store()
    //     {
    //         // 验证接口
    //         $owner_id          = Request::Post('owner_id', 0);

    //         $check_params = array(
    //             // 总店用户id
    //             'owner_id'          => $owner_id,
    //         );
    //         $api_stat_id = api_helper::check_sign($check_params, 1);

    //         _widget('store')->show_user_store();
    //     }

    /**
     * 更新总店信息
     */
    public function updae_user_store() {
        api_helper::return_data(1, '该接口没实现');

        // 验证接口
        //$api_stat_id = api_helper::check_sign(array(), 1);
        api_helper::get_user_info();

        if ($this->user_info['role_type'] == user_config::AGENCY) {
            //如果是 合伙人 身份
            $agency_id = $this->user_id;
            $marketing_id = $this->user_id;
        } elseif ($this->user_info['role_type'] == user_config::MARKETING) {
            //如果是 市场人员 身份
            $arr = _model('user_marketing')->read(array(
                'marketing_id' => $this->user_id
            ));
            $agency_id = $arr['agency_id'];
            $marketing_id = $this->user_id;
        } else {
            api_helper::return_data(1, '身份不对');
        }


        $owner_id = Request::Get('owner_id', 0);
        $name = Request::Post('name', '');
        $mobile = Request::Post('mobile', '');
        $mobile = trim($mobile);

        //$cover = Request::Post('cover', '');
        //$title = Request::Post('title', '');
        $type = Request::Post('type', 0);

        $cert_type = Request::Post('cert_type', -1);
        $cert_idcard_front = Request::Post('cert_idcard_front', '');
        $cert_idcard_back = Request::Post('cert_idcard_back', '');
        $cert_company = Request::Post('cert_company', '');

        if (!$mobile || !$name) {
            api_helper::return_data(1, '参数不能为空');
        }

        //判断 手机号是否存在
        if (!user_helper::is_username_unique($mobile)) {
            api_helper::return_data(1, '手机号已存在');
        }

        //先创建 用户信息
        //$user_id = user_helper::create_user();
        $user_info = array(
            'user_name' => $mobile,
            'realname' => $name,
            'mobile' => $mobile,
        );
        $role_type = user_config::STORE_OWNER;
        $ret_user = user_helper::create_user($user_info, $role_type);

        if (empty($ret_user)) {
            api_helper::return_data(1, '用户创建失败');
        }

        $user_id = $ret_user['user_id'];
        $password = $ret_user['password'];

        //添加总店信息
        $id = _model('user_store')->create(array(
            'user_id' => $user_id,
            'agency_id' => $agency_id,
            'marketing_id' => $marketing_id,

            'cert_type' => $cert_type,
            'cert_idcard_front' => $cert_idcard_front,
            'cert_idcard_back' => $cert_idcard_back,
            'cert_company' => $cert_company,

            'sign_user_id' => $this->user_id,
            'sign_time' => datetime(),
            'add_time' => datetime(),
        ));

        $ret = array(
            'user_id' => $user_id,
            'owner_id' => $user_id,
            'password' => $password,
        );

        if ($id) {
            api_helper::return_data(0, 'success', $ret);
        } else {
            api_helper::return_data(1, '创建出错了');
        }

    }


    /**
     * 创建分店
     */
    public function create_store() {
        // 验证接口
        $owner_id = Request::Post('owner_id', 0);
        $mobile = trim(Request::Post('mobile', ''));
        $name = Request::Post('name', '');
        //获取分店信息
        $title = Request::Post('title', '');
        $title_branch = Request::Post('title_branch', '');
        $type = Request::Post('type', 0);
        $address_code = Request::Post('address_code', -1);
        $address = Request::Post('address', '');
        $longitude = Request::Post('longitude', '');
        $latitude = Request::Post('latitude', '');
        $cover = Request::Post('cover', '');

        $check_params = array(
            'owner_id' => $owner_id,
            'mobile' => $mobile,
            'name' => $name,
            'title' => $title,
            'title_branch' => $title_branch,
            'type' => $type,
            'address_code' => $address_code,
            'address' => $address,
            'longitude' => $longitude,
            'latitude' => $latitude,
            'cover' => $cover
        );

        // ios系统的bug,当定位失败address_code字段未提交，提示接口加密失败
        if ($address_code == -1) {
            unset($check_params['address_code']);
        }

        api_helper::check_sign($check_params, 1);

        $data = _widget('store')->create_store($this->user_id, $owner_id, $mobile, $name, $title, $title_branch, $type, $address_code, $address, $longitude, $latitude, $cover, $this->user_info);
        if (!$data[0]) {
            api_helper::return_data(1, $data[1]);
        } else {
            api_helper::return_data(0, 'success', $data[1]);
        }
    }

    /**
     * 更新分店
     * @author gaoa@alltosun.com
     */
    public function update_store_info() {
        // 验证接口
        $store_id = Request::Post('store_id', 0);
        $name = Request::Post('name', '');
        $mobile = Request::Post('mobile', '');
        $title_branch = Request::Post('title_branch', '');
        $address = Request::Post('address', '');
        $cover = Request::Post('cover', '');
        $is_cover = Request::Post('is_cover', 0);

        $check_params = array(
            'store_id' => $store_id,
            'name' => $name,
            'mobile' => $mobile,
            'title_branch' => $title_branch,
            'address' => $address,
            'cover' => $cover,
            'is_cover' => $is_cover
        );

        $api_stat_id = api_helper::check_sign($check_params, 1);
        $data = _widget('store')->update_store_info($this->user_id, $store_id, $name, $mobile, $title_branch, $address, $cover, $is_cover);
        if (!$data['flag']) {
            api_helper::return_data(1, $data['info']);
        } else {
            api_helper::return_data(0, $data['info']);
        }
    }

    /**
     * 签约总店 new
     * @author gaoa@alltosun.com
     */
    public function create_new_user_store() {
        // 总店信息
        $name = Request::Post('name', '');
        $mobile = trim(Request::Post('mobile', ''));
        $title = Request::Post('title', '');
        $type = Request::Post('type', 0);
        $cert_type = Request::Post('cert_type', 0);
        $cert_idcard_front = Request::Post('cert_idcard_front', '');
        $cert_idcard_back = Request::Post('cert_idcard_back', '');
        $cert_company = Request::Post('cert_company', '');

        // 分店信息
        $title_branch = Request::Post('title_branch', '');
        $address_code = Request::Post('address_code', -1);
        $address = Request::Post('address', '');
        $longitude = Request::Post('longitude', '');
        $latitude = Request::Post('latitude', '');
        $cover = Request::Post('cover', '');

        $check_params = array(
            // 总店店主名
            'name' => $name,
            // 总店手机号
            'mobile' => $mobile,
            // 门店名
            'title' => $title,
            // 认证类型认证类型 0营业执照+身份证 1身份证
            'cert_type' => $cert_type,
            // 身份证正面照
            'cert_idcard_front' => $cert_idcard_front,
            // 身份证背面照
            'cert_idcard_back' => $cert_idcard_back,
            // 营业执照
            'cert_company' => $cert_company,
            'title_branch' => $title_branch,
            'type' => $type,
            'address_code' => $address_code,
            'address' => $address,
            'longitude' => $longitude,
            'latitude' => $latitude,
            'cover' => $cover
        );

        // ios系统的bug,当定位失败address_code字段未提交，提示接口加密失败
        if ($address_code == -1) {
            unset($check_params['address_code']);
        }

        $api_stat_id = api_helper::check_sign($check_params, 1);

        if (!$mobile) {
            api_helper::return_data(1, '手机号不能为空');
        }

        if (!$name) {
            api_helper::return_data(1, '姓名不能为空');
        }

        if (!$title) {
            api_helper::return_data(1, '门店名称不能为空');
        }

        if (!$address) {
            api_helper::return_data(1, '门店地址不能为空');
        }

        if (in_array($type, store_config::$type_arr)) {
            api_helper::return_data(1, '门店类型不对');
        }

        if (!$this->user_id) {
            api_helper::return_data(1, '请登录后操作!');
        }

        //判断 手机号是否存在
        if (!user_helper::is_username_unique($mobile)) {
            api_helper::return_data(1, '手机号已存在');
        }

        // 创建总店
        $user_store_data = _widget('store')->create_new_user_store($this->user_info, $name, $mobile, $title, $type, $cert_type, $cert_idcard_front, $cert_idcard_back, $cert_company);
        if (!$user_store_data[0]) {
            api_helper::return_data(1, $user_store_data[1]);
        } else {
            // 创建分店
            $store_data = _widget('store')->create_store($this->user_id, $user_store_data[1], $mobile, $name, $title, $title_branch, $type, $address_code, $address, $longitude, $latitude, $cover, $this->user_info);
            if (!$store_data[0]) {
                api_helper::return_data(1, $store_data[1]);
            } else {
                api_helper::return_data(0, 'success', $store_data[1]);
            }
        }


    }

    /**
     * 更新分店资料
     */
    public function update_store() {
        // 验证接口
        $mobile = Request::Post('mobile', '');
        $name = Request::Post('name', '');
        $mobile = trim($mobile);
        //获取分店信息
        $title = Request::Post('title', '');
        $title_branch = Request::Post('title_branch', '');
        $type = Request::Post('type', 0);
        $address_code = Request::Post('address_code', 0);
        $address = Request::Post('address', '');
        $longitude = Request::Post('longitude', '');
        $latitude = Request::Post('latitude', '');
        $cover = Request::Post('cover', '');

        $check_params = array(
            'mobile' => $mobile,
            'name' => $name,
            'mobile' => $mobile,
            'title' => $title,
            'title_branch' => $title_branch,
            'type' => $type,
            'address_code' => $address_code,
            'address' => $address,
            'longitude' => $longitude,
            'latitude' => $latitude,
            'cover' => $cover
        );

        $api_stat_id = api_helper::check_sign($check_params, 1);

        api_helper::get_user_info();

        $store_id = Request::Post('store_id', 0);
        $filter = array(
            'id' => $store_id
        );

        $store = store_helper::get_store($store_id);
        if (empty($store)) {
            api_helper::return_data(1, '店铺不存在');
        }

        if ($this->user_info['role_type'] == user_config::AGENCY) {
            //如果是 合伙人 身份
            $agency_id = $this->user_id;
            $filter['agency_id'] = $this->user_id;
        } elseif ($this->user_info['role_type'] == user_config::MARKETING) {
            //如果是 市场人员 身份
            $filter['marketing_id'] = $this->user_id;
        } elseif ($this->user_info['role_type'] == user_config::STORE_OWNER) {
            //如果是 商店老板 身份
            $filter['owner_id'] = $this->user_id;
        } else {
            api_helper::return_data(1, '身份不对');
        }

        /*
        //查询总店信息
        //$user_id = Request::Get('user_id', 0);
        $owner_id = Request::Post('owner_id', 0);

        $user_store = store_helper::get_user_store($owner_id);
        if (empty($user_store)) {
            api_helper::return_data(1, '店铺不存在');
        }

        $owner_info = user_helper::get_user_info($owner_id);
        */
        $store_user = user_helper::get_user_info($store['user_id']);

        $update_arr = array();

        $uparr = array('title', 'title_branch', 'address', 'longitude', 'latitude', 'cover',);
        foreach ($uparr as $k) {
            $v = Request::Post($k, '');
            if (!empty($v)) {
                $update_arr[$k] = trim($v);
            }
        }

        //省市县编码
        $province_id = $city_id = $district_id = 0;
        if ($address_code >= 100000 && $address_code <= 999999) {
            $province_id = substr($address_code, 0, 2) . '0000';
            $city_id = substr($address_code, 0, 4) . '00';
            $district_id = $address_code;

            $update_arr['province_id'] = $province_id;
            $update_arr['city_id'] = $city_id;
            $update_arr['district_id'] = $district_id;
        }

        if ($type > 0 && isset(store_config::$type_arr[$type])) {
            $update_arr['type'] = $type;
        }

        if (empty($update_arr)) {
            api_helper::return_data(1, '请输入要修改的字段');
        }


        //修改分店信息
        $isok = _model('store')->update($filter, $update_arr);

        // 更新user表
        $update_arr = array();
        if ($mobile != $store_user['mobile']) {
            $update_arr['mobile'] = $mobile;
        }
        if ($name != $store_user['realname']) {
            $update_arr['realname'] = $name;
        }

        if (!empty($update_arr)) {
            _model('user')->update(array('id' => $store['user_id']), $update_arr);
        }

        if ($isok) {
            api_helper::return_data(0, 'success');
        } else {
            api_helper::return_data(1, '修改出错了');
        }
    }


    /**
     * 撤店理由列表
     * gaoa add
     */
    public function stop_reason_list() {
        // 验证接口
        $api_stat_id = api_helper::check_sign(array());

        if (!$api_stat_id) {
            api_helper::return_data(1, '请先登录!');
        }

        if ($this->user_info['role_type'] != user_config::AGENCY &&
            $this->user_info['role_type'] != user_config::MARKETING
        ) {
            api_helper::return_data(1, '只有市场人员和合伙人才能撤销门店!');
            return;
        }

        $stop_reason_list = array();
        $stop_reason_list['stop_reason_list'] = store_config::$stop_reason;

        api_helper::return_data(0, 'success', $stop_reason_list);
    }


    /**
     * 暂停总店运营
     */
    public function stop_user_store() {
        //判断是否有权限

        $owner_id = Request::Post('owner_id', 0);
        $stop_reason = Request::Post('stop_reason', '');

        $chekc_params = array(
            'owner_id' => $owner_id,
            'top_reason' => $stop_reason
        );

        $api_stat_id = api_helper::check_sign($chekc_params);

        $user_store = store_helper::get_user_store($owner_id);
        if (empty($user_store)) {
            api_helper::return_data(1, '总店不存在');
        }

        if ($stop_reason == '') {
            api_helper::return_data(1, '请选择撤店理由!');
        }

        $isok = _model('user_store')->update(array(
            'user_id' => $owner_id,
        ), array(
            'status' => store_config::STATUS_STOP,
            'stop_reason' => $stop_reason,
        ));

        //可能不对

        if ($isok) {
            api_helper::return_data(0, 'success');
        } else {
            api_helper::return_data(1, '更新出错了');
        }
    }


    /**
     * 暂停运营
     */
    public function stop_store() {
        //判断是否有权限

        $store_id = Request::Post('store_id', 0);

        $check_params = array(
            'store_id' => $store_id
        );

        $store = store_helper::get_store($store_id);
        if (empty($store)) {
            api_helper::return_data(1, '分店不存在');
        }

        if ($store['status'] == store_config::STATUS_STOP) {
            api_helper::return_data(1, '此店铺已撤店,请勿重复操作.');
        }

        $owner_id = $store['owner_id'];

        //更新分店信息
        $isok = _model('store')->update(array(
            'id' => $store_id,
        ), array(
            'status' => store_config::STATUS_STOP,
            'machine_serial' => '', //回收保温箱
        ));

        // 解除设备绑定
        store_helper::unbind_store_device($store_id);

        //更新总店信息
        $machine_count = _model('store')->getTotal(array(
            'owner_id' => $owner_id,
            'machine_serial !=' => '',
        ));

        $store_count = _model('store')->getTotal(array(
            'owner_id' => $owner_id,
            'status' => array(
                store_config::STATUS_TRIAL,
                store_config::STATUS_NORMAL,
            ),
        ));

        $update_arr = array(
            'machine_num' => $machine_count,
            'store_num' => $store_count,
        );
        if ($store_count <= 0) {
            //$update_arr['status'] = store_config::STATUS_STOP;
        }

        $isok = _model('user_store')->update(array(
            'user_id' => $owner_id,
        ), $update_arr);

        // 伪删除这个分店的店长
        _model('user')->update($store['user_id'], array('status' => 0));

        if ($isok) {

            // 撤店成功发送短信
            _widget('store.message')->stop_store_message($this->user_info, $store);

            api_helper::return_data(0, 'success');
        } else {
            api_helper::return_data(1, '更新出错了');
        }
    }


    /**
     * 恢复运营
     */
    public function recover_store() {
        //判断是否有权限

        $store_id = Request::Post('store_id', 0);

        $check_params = array(
            'store_id' => $store_id
        );

        $store = store_helper::get_store($store_id);
        if (empty($store)) {
            api_helper::return_data(1, '分店不存在');
        }

        if (store_config::STATUS_TRIAL == $store['status']) {
            api_helper::return_data(1, '此门店已恢复运营,请勿重复操作');
        }

        $owner_id = $store['owner_id'];

        //更新分店信息
        $isok = _model('store')->update(array(
            'id' => $store_id,
        ), array(
            'status' => store_config::STATUS_TRIAL,
            'machine_serial' => '', //回收保温箱
        ));

        //更新总店信息
        $machine_count = _model('store')->getTotal(array(
            'owner_id' => $owner_id,
            'machine_serial !=' => '',
        ));

        $store_count = _model('store')->getTotal(array(
            'owner_id' => $owner_id,
            'status' => array(
                store_config::STATUS_TRIAL,
                store_config::STATUS_NORMAL,
            ),
        ));

        $update_arr = array(
            'machine_num' => $machine_count,
            'store_num' => $store_count,
        );
        if ($store_count <= 0) {
            //$update_arr['status'] = store_config::STATUS_STOP;
        }

        $isok = _model('user_store')->update(array(
            'user_id' => $owner_id,
        ), $update_arr);

        // 恢复这个分店的店长
        _model('user')->update($store['user_id'], array('status' => 1));

        if ($isok) {

            // 恢复门店成功发送短信
            _widget('store.message')->recover_store_message($this->user_info, $store);

            api_helper::return_data(0, 'success');
        } else {
            api_helper::return_data(1, '更新出错了');
        }
    }


    /**
     * 修改设备序列号
     */
    public function update_machine_serial() {
        // 验证接口
        //$api_stat_id = api_helper::check_sign(array(), 1);

        if (!$this->user_id) {
            api_helper::return_data(1, '请先登录');
        }

        $store_id = Request::Get('store_id', 0);
        $machine_serial = Request::Get('machine_serial', '');

        $check_params = array(
            'store_id' => $store_id,
            'machine_serial' => $machine_serial
        );
        $api_stat_id = api_helper::check_sign($check_params, 1);

        if ($store_id <= 0) {
            api_helper::return_data(1, '商铺ID不能为空');
        }

        if (empty($machine_serial)) {
            api_helper::return_data(1, '序列号不能为空');
        }

        $store = store_helper::get_store($store_id);

        if (empty($store)) {
            api_helper::return_data(1, '商铺不存在');
        }

        if ($this->user_info['role_type'] == user_config::STORE_MANAGER) {
            //如果是分店店长身份
            $uid = $store['user_id'];
        } elseif ($this->user_info['role_type'] == user_config::STORE_MANAGER) {
            //如果是合伙人
            $uid = $store['owner_id'];
        } elseif ($this->user_info['role_type'] == user_config::AGENCY) {
            //如果是代理商
            $uid = $store['agency_id'];
        } elseif ($this->user_info['role_type'] == user_config::MARKETING) {
            //如果是市场人员
            $uid = $store['marketing_id'];
        } else {
            //api_helper::return_data(1, '身份不对');
        }

        if ($uid != $this->user_id) {
            api_helper::return_data(1, '无权修改');
        }

        if ($store['machine_serial']) {
            api_helper::return_data(1, '店铺已经有序列号');
        }

        $store2 = _uri('store', array('machine_serial' => $machine_serial));
        if (!empty($store2)) {
            api_helper::return_data(1, '序列号已经被<' . $store2['title'] . '>使用');
        }

        $isok = _model('store')->update(array(
            'id' => $store_id
        ), array(
            'machine_serial' => $machine_serial
        ));

        if ($isok) {
            api_helper::return_data(0, 'success');
        } else {
            api_helper::return_data(1, '更新出错了');
        }
    }


    /**
     * 获取总店信息
     */
    public function get_user_store() {
        //判断是否有权限

        //如果是总店店长
        $owner_id = Request::getParam('owner_id', 0);

        // 接口验证
        $check_params = array(
            'owner_id' => $owner_id
        );
        $api_stat_id = api_helper::check_sign($check_params, 1);

        $user_store = store_helper::get_user_store($owner_id);
        if (empty($user_store)) {
            api_helper::return_data(1, '店铺不存在');
        }

        $owner_user = user_helper::get_user_info($owner_id);
        $role_type = user_helper::get_user_role_type($owner_id);

        $sale_money_month = _widget('stat.api_stat')->get_owner_sale_data($owner_id, 'curr_month', 'price');
        $sale_num_month = _widget('stat.api_stat')->get_owner_sale_data($owner_id, 'curr_month', 'box');

        $sale_num_today = _widget('stat.api_stat')->get_owner_sale_data($owner_id, 'curr_day', 'box');
        $sale_money_today = _widget('stat.api_stat')->get_owner_sale_data($owner_id, 'curr_day', 'price');

        $sale_num_yesterday = _widget('stat.api_stat')->get_owner_sale_data($owner_id, 'prev_day', 'box');
        $sale_money_yesterday = _widget('stat.api_stat')->get_owner_sale_data($owner_id, 'prev_day', 'price');

        $type_str = '';
        // 获取门店类型汉字
        if (isset(store_config::$type_arr[$user_store['type']])) {
            $type_str = store_config::$type_arr[$user_store['type']];
        }

        // 根据owner_id获取用户信息
        //         $owner_info = user_helper::get_user_info($owner_id);
        // 获取当前总店id下所有分店的所有积分
        $owner_score = stat_helper::get_user_store_score($owner_id);

        // 获取撤店状态的分店数量
        $stop_store_num = store_helper::get_store_num($owner_id, store_config::STATUS_STOP);

        // 营业执照身份证是否上传
        if ($user_store['cert_company'] == '') {
            $is_have_cert_company = 0;
        } else {
            $is_have_cert_company = 1;
        }

        // 获取签约人信息
        $sign_user_info = user_helper::get_user_info($user_store['sign_user_id']);

        if (!$user_store['cert_idcard_front'] || !$user_store['cert_idcard_back']) {
            $is_have_cert_idcard = 0;
        } else {
            $is_have_cert_idcard = 1;
        }

        $ret = array(
            'owner_id' => $user_store['user_id'],
            'agency_id' => $user_store['agency_id'],
            'marketing_id' => $user_store['marketing_id'],
            'store_num' => $user_store['store_num'],
            'stop_store_num' => $stop_store_num,
            'machine_num' => $user_store['machine_num'],
            'cert_type' => $user_store['cert_type'],
            'cert_company' => user_helper::cert_image($user_store['cert_company']),
            'cert_idcard_front' => user_helper::cert_image($user_store['cert_idcard_front']),
            'cert_idcard_back' => user_helper::cert_image($user_store['cert_idcard_back']),
            // 是否有营业执照
            'is_have_cert_company' => $is_have_cert_company,
            // 是否有身份证
            'is_have_cert_idcard' => $is_have_cert_idcard,
            'score' => $owner_score,
            'type' => $user_store['type'],
            'type_str' => $type_str,
            'title' => $user_store['title'],
            'add_time' => $user_store['add_time'],

            'name' => $owner_user['realname'],
            'mobile' => $owner_user['mobile'],
            'sign_user_name' => $sign_user_info['realname'],
            'sign_user_mobile' => $sign_user_info['mobile'],

            'sale_money_month' => $sale_money_month, // 本月销售金额
            'sale_num_month' => $sale_num_month, // 本月销售箱数

            'sale_num_today' => $sale_num_today, //今日销售数量
            'sale_money_today' => $sale_money_today, //今日销售金额

            'sale_num_yesterday' => $sale_num_yesterday, //昨日销售数量
            'sale_money_yesterday' => $sale_money_yesterday, //昨日销售金额


        );
        api_helper::return_data(0, 'success', $ret);

        /*
        $list = _model('store')->getList(array(
            'owner_id'  => $owner_id,
            'status !=' => 0,
        ));

        $ret = $user_store;
        //身份证照片

        foreach ($list as $k => $v) {
            $ret['stores'][] = $v;
            //分店头图
        }

        api_helper::return_data(0, 'success', $ret);
        */
    }

    /**
     * 获取分店信息
     */
    public function get_store() {
        //判断是否有权限

        //如果是总店店长

        $store_id = Request::getParam('store_id', 0);

        // 接口验证
        $check_params = array(
            'store_id' => $store_id
        );

        $auth_arr = store_helper::check_store_auth($this->user_info, $store_id);
        if (!$auth_arr[0]) {
            api_helper::return_data(1, $auth_arr[1]);
        }

        $store = store_helper::get_store($store_id);
        if (empty($store)) {
            api_helper::return_data(0, 'success', array());
        }

        $manager_id = $store['user_id'];
        $store_user = user_helper::get_user_info($manager_id);

        $role_type = user_helper::get_user_role_type($manager_id);

        // $sale_arr = stat_helper::get_agency_month_sale($role_type, $manager_id);
        // $sale_arr_today = stat_helper::get_agency_daily_sale($role_type, $manager_id);
        // $sale_arr_yesterday = stat_helper::get_agency_yesterday_sale($role_type, $manager_id);

        $sale_num_month = _widget('stat.api_stat')->get_store_sale_data($manager_id, 'curr_month', 'box');
        $sale_money_month = _widget('stat.api_stat')->get_store_sale_data($manager_id, 'curr_month', 'price');

        $sale_num_today = _widget('stat.api_stat')->get_store_sale_data($manager_id, 'curr_day', 'box');
        $sale_money_today = _widget('stat.api_stat')->get_store_sale_data($manager_id, 'curr_day', 'price');

        $sale_num_yesterday = _widget('stat.api_stat')->get_store_sale_data($manager_id, 'prev_day', 'box');
        $sale_money_yesterday = _widget('stat.api_stat')->get_store_sale_data($manager_id, 'prev_day', 'price');

        // 获取门店类型汉字
        $type_str = '';
        if (isset(store_config::$type_arr[$store['type']])) {
            $type_str = store_config::$type_arr[$store['type']];
        }

        // 获取签约人信息
        $sign_user_info = user_helper::get_user_info($store['sign_user_id']);

        $ret = array(
            'store_id' => $store['id'],
            'title' => $store['title'],
            'title_branch' => $store['title_branch'],
            'cover' => store_helper::cover($store['cover']),
            'address' => $store['address'],
            'address_code' => $store['district_id'],
            'longitude' => $store['longitude'],
            'latitude' => $store['latitude'],
            'score' => $store['score'],
            'level' => $store['level'],
            'type' => $store['type'],
            'type_str' => $type_str,
            'status' => $store['status'],
            'store_order' => $store['store_order'],

            'name' => $store_user['realname'],
            'mobile' => $store_user['mobile'],
            'sign_user_name' => $sign_user_info['realname'],
            'sign_user_mobile' => $sign_user_info['mobile'],
            'sign_user_id' => $sign_user_info['id'],

            // 'sale_money_month'     => $sale_arr['purchase_money'], // 本月销售金额
            // 'sale_num_month'       => $sale_arr['purchase_num'], // 本月销售箱数

            // 'sale_num_today'       => $sale_arr_today['purchase_num'], //今日销售数量
            // 'sale_money_today'     => $sale_arr_today['purchase_money'], //今日销售金额

            // 'sale_num_yesterday'   => $sale_arr_yesterday['purchase_num'], //昨日销售数量
            // 'sale_money_yesterday' => $sale_arr_yesterday['purchase_money'], //昨日销售金额

            'sale_money_month' => $sale_money_month, // 本月销售金额
            'sale_num_month' => $sale_num_month, // 本月销售箱数

            'sale_num_today' => $sale_num_today, //今日销售数量
            'sale_money_today' => $sale_money_today, //今日销售金额

            'sale_num_yesterday' => $sale_num_yesterday, //昨日销售数量
            'sale_money_yesterday' => $sale_money_yesterday, //昨日销售金额

        );

        // 获取设备列表详情
        $ret['device_list'] = device_helper::get_store_all_device_list_by_store_id($store_id);

        api_helper::return_data(0, 'success', $ret);
    }

    /**
     * 获取 总店老板/店长的所有分店信息
     */
    public function list_my_stores() {
        // 验证接口
        $api_stat_id = api_helper::check_sign(array(), 1);
        api_helper::get_user_info();

        if (!$this->user_id) {
            api_helper::return_data(1, '请先登录');
        }

        if ($this->user_info['role_type'] == user_config::STORE_OWNER) {
            //如果是总店老板身份
            $uid = $this->user_id;
            $filter['owner_id'] = $this->user_id;
        } elseif ($this->user_info['role_type'] == user_config::STORE_MANAGER) {
            //如果是分店店长身份
            $uid = $this->user_id;
            $filter['user_id'] = $this->user_id;
        } else {
            api_helper::return_data(1, '身份不对');
        }

        $list = _model('store')->getList($filter);

        $user_store_title = NULL;
        $stores = array();
        foreach ($list as $store) {
            if ($user_store_title === NULL) {
                //总店标题
                $user_store = store_helper::get_user_store($store['owner_id']);
                $user_store_title = $user_store['title'] . '';
            }

            $stores[] = array(
                'store_id' => $store['id'],
                'title' => $store['title'],
                'title_branch' => $store['title_branch'],
                'user_store_title' => $user_store_title,
                'cover' => store_helper::cover($store['cover']),
                'address' => $store['address'],
                'address_code' => $store['district_id'],
                'longitude' => $store['longitude'],
                'latitude' => $store['latitude'],
                'score' => $store['score'],
                'level' => $store['level'],
                'status' => $store['status'],
            );
        }
        api_helper::return_data(0, 'success', $stores);

        /*
        $ret = array(
            'user_store' => array(
                'owner_id'    => $user_store['user_id'],
                'agency_id'   => $user_store['agency_id'],
                'store_num'   => $user_store['store_num'],
                'machine_num' => $user_store['machine_num'],
                'add_time'    => $user_store['add_time'],
            ),
            'stores'     => $stores,
        );
        api_helper::return_data(0, 'success', $ret);
        */
    }

    /**
     * 区域管理 选择终端类型页面
     */
    public function list_stores_digest() {
        // 验证接口
        $api_stat_id = api_helper::check_sign(array(), 1);
        api_helper::get_user_info();

        if (!$this->user_id) {
            api_helper::return_data(1, '请先登录');
        }

        $where = '';
        if ($this->user_info['role_type'] == user_config::STORE_MANAGER) {
            //如果是分店店长身份
            $where .= ' and user_id = ' . $this->user_id;
        } elseif ($this->user_info['role_type'] == user_config::STORE_OWNER) {
            //如果是合伙人
            $where .= ' and owner_id = ' . $this->user_id;
        } elseif ($this->user_info['role_type'] == user_config::AGENCY) {
            //如果是合伙人
            $where .= ' and agency_id = ' . $this->user_id;
        } elseif ($this->user_info['role_type'] == user_config::MARKETING) {
            //如果是市场人员
            $where .= ' and marketing_id = ' . $this->user_id;
        } else {
            api_helper::return_data(1, '身份不对');
        }

        $only_chain_shop = Request::getParam('only_chain_shop', 0);
        if ($only_chain_shop) {
            $where .= ' and store_num > 1';
        }


        $arr = _model('store')->getAll('select type, count(*) as cnt from store where status != 0 ' .
                                       $where .
                                       ' group by `type`');
        if (!empty($arr)) {
            //重排
            foreach ($arr as $v) {
                $type = $v['type'];
                $arr2[$type] = $v['cnt'];
            }

            $all = 0;
            $ret[0] = array(
                'type' => 0,
                'name' => '全部终端',
                'count' => 0,
            );
            foreach (store_config::$type_arr as $type => $name) {
                if (isset($arr2[$type])) {
                    $ret[] = array(
                        'type' => $type,
                        'name' => $name,
                        'count' => $arr2[$type],
                    );

                    $all += $arr2[$type];
                }
            }

            $ret[0]['count'] = $all;

        }

        api_helper::return_data(0, 'success', $ret);
    }


    /**
     * 区域管理 根据终端类型等选择
     */
    public function list_stores_info_by_type() {
        // 验证接口
        $page_size = Request::getParam('page_size', 20);
        $page = Request::getParam('page_no', 1);
        $status = Request::getParam('status', -1);
        $only_chain_shop = Request::getParam('only_chain_shop', 0);
        $type = Request::getParam('type', 0);

        $check_params = array(
            'page_size' => $page_size,
            'page_no' => $page,
            'status' => $status,
            'only_chain_shop' => 'only_chain_shop',
            'type' => $type
        );
        $api_stat_id = api_helper::check_sign($check_params, 1);

        api_helper::get_user_info();

        if (!$this->user_id) {
            api_helper::return_data(1, '请先登录');
        }

        $filter = array();
        if ($this->user_info['role_type'] == user_config::STORE_OWNER) {
            //如果是总店身份
            $filter['owner_id'] = $this->user_id;
        } elseif ($this->user_info['role_type'] == user_config::AGENCY) {
            //如果是合伙人
            $filter['agency_id'] = $this->user_id;
        } elseif ($this->user_info['role_type'] == user_config::MARKETING) {
            //如果是市场人员
            $filter['marketing_id'] = $this->user_id;
        } else {
            //api_helper::return_data(1, '身份不对');
        }

        if ($only_chain_shop) {
            $filter['store_num >'] = 1;
        }

        if ($type > 0) {
            $filter['type'] = $type;
        }

        if ($status > -1) {
            $filter['status'] = $status;
        } else {
            $filter['status !='] = 0;
        }


        $return_list = array();

        $store_count = _model('store')->getTotal($filter);


        if ($store_count) {
            if ($page_size > 0) {
                $pager = new Pager($page_size);
                $store_list = _model('store')->getList($filter, $pager->getLimit($page));
            } else {
                $store_list = _model('store')->getList($filter);
            }

            foreach ($store_list as $k => $store) {
                $info = array(
                    'store_id' => $store['id'],
                    'title' => $store['title'],
                    'title_branch' => $store['title_branch'],
                    'cover' => store_helper::cover($store['cover']),
                    'address' => $store['address'],
                    'address_code' => $store['district_id'],
                    'longitude' => $store['longitude'],
                    'latitude' => $store['latitude'],
                    'score' => $store['score'],
                    'level' => $store['level'],
                    'status' => $store['status'],
                );

                //积分
                $score = score_record_helper::get_user_score($store['user_id']);
                $info['score'] = $score;

                //分店状态
                $status_msg = '';
                if ($store['status'] == store_config::STATUS_NORMAL) {
                    $status_msg = '';
                } elseif ($store['status'] == store_config::STATUS_NORMAL) {
                    $status_msg = '正在等待审核';
                } elseif ($store['status'] == store_config::STATUS_STOP) {
                    $status_msg = '营业执照过期'; //TODO:
                }
                $info['status_msg'] = $status_msg;

                $user_info = user_helper::get_user_info($store['user_id']);
                //$info['user_info']['user_name'] = $user_info['user_name'];
                $info['manager_info']['nickname'] = $user_info['nickname'];
                $info['manager_info']['realname'] = $user_info['realname'];

                //销售额
                $info['sale_info'] = array(
                    'money' => '1000',
                    'number' => '20',
                );


                //封面图
                if ($store['cover']) {
                    $info['cover'] = store_helper::cover($store['cover']);
                }

                $return_list[$k] = $info;

            }
        }

        api_helper::return_data(0, 'success', $return_list);

    }

    /**
     * 获取 合伙人的所有店铺信息(总店和第一分店)
     */
    public function list_agency_user_stores() {
        // 验证接口
        $page_size = Request::getParam('page_size', '0');
        $page = Request::getParam('page_no', 1);
        $status = Request::getParam('status', '-1');
        // 只查连锁店
        $only_chain_shop = Request::getParam('only_chain_shop', '0');
        $type = Request::getParam('type', '0');
        $marketing_id = Request::getParam('marketing_id', 0);
        $dealer_id = Request::getParam('dealer_id', 0);
        $is_unset = Request::getParam('unset', 0);
        $realname_or_title = Request::getParam('realname_or_title', '');
        $v = Request::getParam('v', 0);

        $check_params = array(
            'page_size' => $page_size,
            'page' => $page,
            'status' => $status,
            'only_chain_shop' => $only_chain_shop,
            'type' => $type
        );

        // 兼容老接口
        if ($v > 1.6) {
            $page_size = 10;
        } else {
            $page_size = 1000;
        }

        api_helper::check_sign($check_params, 1);
        api_helper::get_user_info();

        if ($this->user_info['role_type'] == user_config::AGENCY) {
            // 如果是合伙人
            $filter['agency_id'] = $this->user_id;

            if ($marketing_id && $marketing_id != $dealer_id) {
                $filter['marketing_id'] = $marketing_id;
            }

        } elseif ($this->user_info['role_type'] == user_config::MARKETING) {
            // 如果是市场人员
            $filter['marketing_id'] = $this->user_id;
            $filter['sign_user_id'] = $this->user_id;

        } else {
            api_helper::return_data(1, '身份不对');
        }

        if (isset(store_config::$type_arr[$type])) {
            $filter['type'] = $type;
        }

        // 未改价的店铺列表，合伙人和市场人员都有
        $user_store_ids = array();
        if ($this->user_info['role_type'] == user_config::AGENCY) {
            $user_store_ids = dealers_helper::get_unset_price_store_list($this->user_id, 0);
            $unset_device_store_ids = dealers_helper::get_unset_device_store_list($this->user_id, $marketing_id);
        } else {
            if ($this->user_info['role_type'] == user_config::MARKETING) {
                $user_store_ids = dealers_helper::get_unset_price_store_list(0, $this->user_id);
                $unset_device_store_ids = dealers_helper::get_unset_device_store_list(0, $this->user_id);
            }
        }

        if ($is_unset) {
            if (!$user_store_ids) {
                api_helper::return_data(1, '暂无门店信息');
            }
            $filter['id'] = array_unique(array_merge($user_store_ids, $unset_device_store_ids));
        }

        if ($realname_or_title) {
            $filter['(`title` LIKE '] = '%' . $realname_or_title . '%';
            $search_user_ids = _model('user')->getFields('id', array(
                '`realname` LIKE ' => '%' . $realname_or_title . '%'
            ));
            if ($search_user_ids) {
                $filter['OR `user_id`'] = $search_user_ids;
            } else {
                $filter['OR `user_id`'] = 0;
            }
            if ($status != -1) {
                $filter[') and `status`'] = $status;
                $status_store_filter = array_merge($filter, array('is_delete' => 1));
                $status_owner_ids = get_app_fields('store', 'owner_id', $filter, 'ORDER BY `id` DESC', $page, $page_size);
            } else {
                $filter[') and `status`'] = array(1, 2, 3);
            }
        } else {
            if ($status != -1) {
                $filter['status'] = $status;
                $status_store_filter = array_merge($filter, array('is_delete' => 1));

                $status_owner_ids = get_app_fields('store', 'owner_id', $filter, 'ORDER BY `id` DESC', $page, $page_size);
            } else {
                $filter['status'] = array(1, 2, 3);
            }
        }

        $fail_arr = array();
        if (!isset($status_owner_ids)) {
            $owner_id_fields = get_app_fields('user_store', 'user_id', $filter, 'ORDER BY `id` DESC', $page, $page_size);
        } else {
            $owner_id_fields = $status_owner_ids;
        }
        if (!$owner_id_fields) {
            api_helper::return_data(1, '暂无门店信息');
        }

        $owner_ids = $owner_id_fields['fields'];
        //         print_r($owner_ids);
        $user_store_filter = array('user_id' => $owner_ids);

        $owner_filter = array('id' => $owner_ids);
        if ($status == -1) {
            $store_filter = array('owner_id' => $owner_ids);
        } else {
            $store_filter = array('owner_id' => $owner_ids, 'status' => $status);
        }

        // 总店列表
        $user_store_list = _model('user_store')->getList($user_store_filter);

        // 总店店主列表
        $owner_info_list = _model('user')->getList($owner_filter);

        // 第一家分店列表
        $store_list = _model('store')->getList($store_filter, ' ORDER BY `store_order`');

        // 获取所有第一家分店店主用户信息
        $store_user_ids = _model('store')->getFields('user_id', $store_filter);
        $store_user_filter = array('id' => $store_user_ids);
        $store_user_list = _model('user')->getList($store_user_filter);

        $store_user_list = api_helper::array_to_filelds_assoc('id', $store_user_list);
        $owner_info_list = api_helper::array_to_filelds_assoc('id', $owner_info_list);
        $store_list = api_helper::array_to_filelds_assoc('owner_id', $store_list);

        // 签约人ids
        $marketing_ids = array();

        foreach ($user_store_list as $arr) {
            if (!in_array($arr['marketing_id'], $marketing_ids) && $arr['marketing_id']) {
                $marketing_ids[] = $arr['marketing_id'];
            }
        }

        if ($marketing_ids) {
            // 签约人列表
            $marketing_list = _model('user')->getList($marketing_ids);
            $marketing_list = api_helper::array_to_filelds_assoc('id', $marketing_list);
        }


        // 先查 总店信息
        $ret = array();

        foreach ($user_store_list as $arr) {
            $owner_id = $arr['user_id'];
            $marketing_id = $arr['marketing_id'];
            if (!isset($owner_info_list[$owner_id]) ||
                !isset($store_list[$owner_id]) ||
                !isset($marketing_list[$marketing_id])
            ) {
                $fail_arr[] = $owner_id;
                continue;
            }

            $owner_info = $owner_info_list[$owner_id];
            $marketing_info = $marketing_list[$marketing_id];
            $store = $store_list[$owner_id];
            if (isset($store_user_list[$store['user_id']])) {
                $store_user = $store_user_list[$store['user_id']];
            } else {
                continue;
            }
            $store_num_filter = array();
            $store_num_filter['owner_id'] = $owner_id;
            // 获取当前总店id下所有分店的所有积分
            $owner_score = stat_helper::get_user_store_score($store['owner_id']);

            // 计算店铺等级
            // $level = score_record_helper::get_user_score_level($owner_score);
            $store_info = array(
                'store_id' => $store['id'],
                'title' => $store['title'],
                //'title_branch' => $store['title_branch'],
                'cover' => store_helper::cover($store['cover']),
                // 'address'      => $store['address'],
                // 'address_code' => $store['district_id'],
                // 'longitude'    => $store['longitude'],
                // 'latitude'     => $store['latitude'],
                'score' => $owner_score,
                // 'level'        => $level,
                'status' => $store['status'],

                //'name'         => empty($store_user['realname']) ? $owner_info['realname'] : $store_user['realname'],
                'mobile' => empty($store_user['mobile']) ? $owner_info['mobile'] : $store_user['mobile'],
            );

            //总进货信息
            // $purchase_info = stat_helper::get_owner_all_purchase($arr['user_id']);
            $purchase_num = _widget('stat.api_stat')->get_owner_sale_data($arr['user_id'], 'curr_month', 'box');
            $purchase_money = _widget('stat.api_stat')->get_owner_sale_data($arr['user_id'], 'curr_month', 'price');
            $purchase_money = num_keep_two_round($purchase_money);

            // author:xiaolin date:2016-11-01
            if ($this->user_info['role_type'] == user_config::AGENCY) {
                $store_num_filter['agency_id'] = $this->user_id;
            } elseif ($this->user_info['role_type'] == user_config::MARKETING) {
                $store_num_filter['sign_user_id'] = $this->user_id;
            }

            // author:xiaolin date:2016-11-01
            if ($status != -1) {
                $store_num_filter['status'] = $status;
            } else {
                $store_num_filter['status != '] = 0;
            }

            // 同价门店数量
            $arr['store_num'] = _model('store')->getTotal($store_num_filter);
            // 获取撤店数量
            $stop_store_num = store_helper::get_store_num($owner_id, store_config::STATUS_STOP);

            $ret[] = array(
                'user_store' => array(
                    // 'store_id'       => $store_info['store_id'],
                    'owner_id' => $arr['user_id'],
                    'agency_id' => $arr['agency_id'],
                    'store_num' => $arr['store_num'],
                    'stop_store_num' => $stop_store_num,
                    'machine_num' => $arr['machine_num'],
                    'device_num' => device_helper::get_bind_device_count_by_owner_id($owner_id),
                    'add_time' => $arr['add_time'],


                    'name' => $owner_info['realname'],
                    // 'mobile'         => $owner_info['mobile'],

                    'marketing_info' => array(
                        'user_id' => $arr['marketing_id'],
                        'name' => $marketing_info['realname'] . '',
                        //'mobile'  => $marketing_info['mobile'] . '',
                    ),

                    'purchase_info' => array(
                        'number' => $purchase_num,
                        'money' => $purchase_money,
                    ),
                ),
                'store' => (object)$store_info

            );


        }


        $result = $ret;

        if ($v > 1.6) {
            $result = array(
                'data_list' => $ret,

                'data_list_count' => count($ret),
                'data_list_c' => count($owner_ids),

                'fail_ids' => $fail_arr,
                'pages' => $owner_id_fields['page'],

                // 未调价
                'unset_price_store_num' => (string)count($user_store_ids),
                // 未绑定设备，未调价
                'unset_device_store_num' => (string)count($unset_device_store_ids)
            );
        }

        if (!count($ret)) {
            api_helper::return_data(1, '暂无门店信息');
        } else {
            api_helper::return_data(0, 'success', $result);
        }
    }


    /**
     * 获取 合伙人的所有店铺信息(总店和第一分店)
     */
    public function list_agency_user_stores3() {
        // 验证接口
        $page_size = Request::getParam('page_size', '0');
        $page = Request::getParam('page_no', 1);
        $status = Request::getParam('status', '-1');
        // 只查连锁店
        $only_chain_shop = Request::getParam('only_chain_shop', '0');
        $type = Request::getParam('type', '0');
        $marketing_id = Request::getParam('marketing_id', 0);
        $dealer_id = Request::getParam('dealer_id', 0);
        $is_unset = Request::getParam('unset', 0);
        $title = Request::getParam('title', '');
        $v = Request::getParam('v', 0);

        $check_params = array(
            'page_size' => $page_size,
            'page' => $page,
            'status' => $status,
            'only_chain_shop' => $only_chain_shop,
            'type' => $type
        );

        // 兼容老接口
        if ($v > 1.6) {
            $page_size = 10;
        } else {
            $page_size = 1000;
        }

        $api_stat_id = api_helper::check_sign($check_params, 1);
        api_helper::get_user_info();

        if ($this->user_info['role_type'] == user_config::AGENCY) {
            // 如果是合伙人
            $uid = $this->user_id;
            $filter['agency_id'] = $this->user_id;

            if ($marketing_id) {
                $filter['marketing_id'] = $marketing_id;
            }

        } elseif ($this->user_info['role_type'] == user_config::MARKETING) {
            // 如果是市场人员
            $uid = $this->user_id;
            $filter['marketing_id'] = $this->user_id;
            $filter['sign_user_id'] = $this->user_id;

        } else {
            api_helper::return_data(1, '身份不对');
        }

        if (isset(store_config::$type_arr[$type])) {
            $filter['type'] = $type;
        }

        // 未改价的店铺列表，合伙人和市场人员都有
        $user_store_ids = array();
        if ($this->user_info['role_type'] == user_config::AGENCY) {
            $user_store_ids = dealers_helper::get_unset_price_store_list($this->user_id, 0);
            $unset_device_store_ids = dealers_helper::get_unset_device_store_list($this->user_id, $marketing_id);
        } else {
            if ($this->user_info['role_type'] == user_config::MARKETING) {
                $user_store_ids = dealers_helper::get_unset_price_store_list(0, $this->user_id);
                $unset_device_store_ids = dealers_helper::get_unset_device_store_list(0, $this->user_id);
            }
        }

        if ($is_unset) {
            if (!$user_store_ids) {
                api_helper::return_data(1, '暂无门店信息');
            }
            $filter = $user_store_ids;
        }

        if ($status != -1) {
            $filter['status'] = $status;
        } else {
            $filter['status'] = array(1, 2, 3);
        }

        if ($title) {
            $filter['`title` LIKE '] = '%' . $title . '%';
        }

        $fail_arr = array();
        $user_store_arr = get_app_data_list('user_store', $filter, 'ORDER BY `id` DESC', $page, $page_size);
        if (!$user_store_arr) {
            api_helper::return_data(1, '暂无门店信息');
        }
        //      待优化吧
        //         foreach ($user_store_arr['data'] as $arr) {
        //                 $owner_ids[] = $arr['user_id'];
        //                 $marketing_id = $arr['marketing_id'];
        //         }

        // 先查 总店信息
        $ret = array();
        foreach ($user_store_arr['data'] as $arr) {

            $owner_id = $arr['user_id'];
            $owner_info = user_helper::get_user_info($owner_id);

            if (empty($owner_info)) {
                continue;
                $fail_arr[] = $arr['user_id'];
            }

            $filter_store = array(
                'owner_id' => $owner_id,
            );

            $store_num_filter = array();
            $store_num_filter['owner_id'] = $owner_id;

            if ($status != -1) {
                $filter_store['status'] = $status;
                $store_num_filter['status'] = $status;
            }

            $store = _model('store')->read($filter_store, 'order by id ASC', ' LIMIT 0, 1');

            $store_info = array();
            if (!empty($store)) {
                $store_user = user_helper::get_user_info($store['user_id']);

                if (!$store_user) {
                    continue;
                }

                // 获取当前总店id下所有分店的所有积分
                $owner_score = stat_helper::get_user_store_score($store['owner_id']);

                // 计算店铺等级
                // $level = score_record_helper::get_user_score_level($owner_score);
                $store_info = array(
                    'store_id' => $store['id'],
                    'title' => $store['title'],
                    //'title_branch' => $store['title_branch'],
                    'cover' => store_helper::cover($store['cover']),
                    // 'address'      => $store['address'],
                    // 'address_code' => $store['district_id'],
                    // 'longitude'    => $store['longitude'],
                    // 'latitude'     => $store['latitude'],
                    'score' => $owner_score,
                    // 'level'        => $level,
                    'status' => $store['status'],

                    //'name'         => empty($store_user['realname']) ? $owner_info['realname'] : $store_user['realname'],
                    // 'mobile'       => empty($store_user['mobile']) ? $owner_info['mobile'] : $store_user['mobile'],
                );
            } else {
                $fail_arr[] = $arr['user_id'];
                continue;
            }

            //合伙人信息
            $marketing_info = user_helper::get_user_info($arr['marketing_id']);
            if (!$marketing_info) {
                continue;
            }

            //总进货信息
            // $purchase_info = stat_helper::get_owner_all_purchase($arr['user_id']);
            $purchase_num = _widget('stat.api_stat')->get_owner_sale_data($arr['user_id'], 'curr_month', 'box');
            $purchase_money = _widget('stat.api_stat')->get_owner_sale_data($arr['user_id'], 'curr_month', 'price');
            $purchase_money = num_keep_two_round($purchase_money);


            if ($this->user_info['role_type'] == user_config::MARKETING) {
                $store_num_filter['sign_user_id'] = $this->user_id;
            }
            // 同价门店数量
            $arr['store_num'] = _model('store')->getTotal($store_num_filter);
            // 获取撤店数量
            $stop_store_num = store_helper::get_store_num($owner_id, store_config::STATUS_STOP);

            $ret[] = array(
                'user_store' => array(
                    // 'store_id'       => $store_info['store_id'],
                    'owner_id' => $arr['user_id'],
                    'agency_id' => $arr['agency_id'],
                    'store_num' => $arr['store_num'],
                    'stop_store_num' => $stop_store_num,
                    'machine_num' => $arr['machine_num'],
                    'device_num' => device_helper::get_bind_device_count_by_owner_id($owner_id),
                    'add_time' => $arr['add_time'],


                    'name' => $owner_info['realname'],
                    // 'mobile'         => $owner_info['mobile'],

                    'marketing_info' => array(
                        'user_id' => $arr['marketing_id'],
                        'name' => $marketing_info['realname'] . '',
                        //'mobile'  => $marketing_info['mobile'] . '',
                    ),

                    'purchase_info' => array(
                        'number' => $purchase_num,
                        'money' => $purchase_money,
                    ),
                ),
                'store' => (object)$store_info

            );
        }

        $result = $ret;

        if ($v > 1.6) {
            $result = array(
                'data_list' => $ret,

                'data_list_count' => count($ret),
                'data_list_c' => count($user_store_arr['data']),

                'fail_ids' => $fail_arr,
                'pages' => $user_store_arr['page'],

                // 未调价
                'unset_price_store_num' => (string)count($user_store_ids),
                // 未绑定设备，未调价
                'unset_device_store_num' => (string)count($unset_device_store_ids)
            );
        }

        if (!count($ret)) {
            api_helper::return_data(1, '暂无门店信息');
        } else {
            api_helper::return_data(0, 'success', $result);
        }
    }

    /**
     * 获取 合伙人的所有店铺信息(总店和第一分店)旧版
     */
    public function list_agency_user_stores_bak() {
        // 验证接口
        $page_size = Request::getParam('page_size', '0');
        $page = Request::getParam('page_no', 1);
        $status = Request::getParam('status', '-1');
        // 只查连锁店
        $only_chain_shop = Request::getParam('only_chain_shop', '0');
        $type = Request::getParam('type', '0');
        $marketing_id = Request::getParam('marketing_id', 0);
        $dealer_id = Request::getParam('dealer_id', 0);
        $is_unset = Request::getParam('unset', 0);
        $title = Request::getParam('title', '');
        $v = Request::getParam('v', 0);

        $check_params = array(
            'page_size' => $page_size,
            'page' => $page,
            'status' => $status,
            'only_chain_shop' => $only_chain_shop,
            'type' => $type
        );

        // 兼容老接口
        if ($v > 1.6) {
            $page_size = 10;
        } else {
            $page_size = 1000;
        }

        $api_stat_id = api_helper::check_sign($check_params, 1);
        api_helper::get_user_info();

        if ($this->user_info['role_type'] == user_config::AGENCY) {
            // 如果是合伙人
            $uid = $this->user_id;
            $filter['agency_id'] = $this->user_id;

            if ($marketing_id) {
                $filter['marketing_id'] = $marketing_id;
            }

        } elseif ($this->user_info['role_type'] == user_config::MARKETING) {
            // 如果是市场人员
            $uid = $this->user_id;
            $filter['marketing_id'] = $this->user_id;
            $filter['sign_user_id'] = $this->user_id;

        } else {
            api_helper::return_data(1, '身份不对');
        }

        if (isset(store_config::$type_arr[$type])) {
            $filter['type'] = $type;
        }

        // 未改价的店铺列表，合伙人和市场人员都有
        $user_store_ids = array();
        if ($this->user_info['role_type'] == user_config::AGENCY) {
            $user_store_ids = dealers_helper::get_unset_price_store_list($this->user_id, 0);
            $unset_device_store_ids = dealers_helper::get_unset_device_store_list($this->user_id, $marketing_id);
        } else {
            if ($this->user_info['role_type'] == user_config::MARKETING) {
                $user_store_ids = dealers_helper::get_unset_price_store_list(0, $this->user_id);
                $unset_device_store_ids = dealers_helper::get_unset_device_store_list(0, $this->user_id);
            }
        }

        if ($is_unset) {
            if (!$user_store_ids) {
                api_helper::return_data(1, '暂无门店信息');
            }
            $filter = $user_store_ids;
        }

        if ($status != -1) {
            $filter['status'] = $status;
        } else {
            $filter['status'] = array(1, 2, 3);
        }

        if ($title) {
            $filter['`title` LIKE '] = '%' . $title . '%';
        }

        $fail_arr = array();
        $user_store_arr = get_app_data_list('user_store', $filter, 'ORDER BY `id` DESC', $page, $page_size);
        if (!$user_store_arr) {
            api_helper::return_data(1, '暂无门店信息');
        }
        //      待优化吧
        foreach ($user_store_arr['data'] as $arr) {
            $owner_ids[] = $arr['user_id'];
            $marketing_id = $arr['marketing_id'];
        }

        // 先查 总店信息
        $ret = array();
        foreach ($user_store_arr['data'] as $arr) {

            $owner_id = $arr['user_id'];
            $owner_info = user_helper::get_user_info($owner_id);

            if (empty($owner_info)) {
                continue;
                $fail_arr[] = $arr['user_id'];
            }

            $filter_store = array(
                'owner_id' => $owner_id,
            );

            $store_num_filter = array();
            $store_num_filter['owner_id'] = $owner_id;

            if ($status != -1) {
                $filter_store['status'] = $status;
                $store_num_filter['status'] = $status;
            }

            $store = _model('store')->read($filter_store, 'order by id ASC', ' LIMIT 0, 1');

            $store_info = array();
            if (!empty($store)) {
                $store_user = user_helper::get_user_info($store['user_id']);

                if (!$store_user) {
                    continue;
                }

                // 获取当前总店id下所有分店的所有积分
                $owner_score = stat_helper::get_user_store_score($store['owner_id']);

                // 计算店铺等级
                // $level = score_record_helper::get_user_score_level($owner_score);
                $store_info = array(
                    'store_id' => $store['id'],
                    'title' => $store['title'],
                    //'title_branch' => $store['title_branch'],
                    'cover' => store_helper::cover($store['cover']),
                    // 'address'      => $store['address'],
                    // 'address_code' => $store['district_id'],
                    // 'longitude'    => $store['longitude'],
                    // 'latitude'     => $store['latitude'],
                    'score' => $owner_score,
                    // 'level'        => $level,
                    'status' => $store['status'],

                    //'name'         => empty($store_user['realname']) ? $owner_info['realname'] : $store_user['realname'],
                    'mobile' => empty($store_user['mobile']) ? $owner_info['mobile'] : $store_user['mobile'],
                );
            } else {
                $fail_arr[] = $arr['user_id'];
                continue;
            }

            //合伙人信息
            $marketing_info = user_helper::get_user_info($arr['marketing_id']);
            if (!$marketing_info) {
                continue;
            }

            //总进货信息
            // $purchase_info = stat_helper::get_owner_all_purchase($arr['user_id']);
            $purchase_num = _widget('stat.api_stat')->get_owner_sale_data($arr['user_id'], 'curr_month', 'box');
            $purchase_money = _widget('stat.api_stat')->get_owner_sale_data($arr['user_id'], 'curr_month', 'price');
            $purchase_money = num_keep_two_round($purchase_money);


            if ($this->user_info['role_type'] == user_config::MARKETING) {
                $store_num_filter['sign_user_id'] = $this->user_id;
            }
            // 同价门店数量
            $arr['store_num'] = _model('store')->getTotal($store_num_filter);
            // 获取撤店数量
            $stop_store_num = store_helper::get_store_num($owner_id, store_config::STATUS_STOP);

            $ret[] = array(
                'user_store' => array(
                    // 'store_id'       => $store_info['store_id'],
                    'owner_id' => $arr['user_id'],
                    'agency_id' => $arr['agency_id'],
                    'store_num' => $arr['store_num'],
                    'stop_store_num' => $stop_store_num,
                    'machine_num' => $arr['machine_num'],
                    'device_num' => device_helper::get_bind_device_count_by_owner_id($owner_id),
                    'add_time' => $arr['add_time'],


                    'name' => $owner_info['realname'],
                    // 'mobile'         => $owner_info['mobile'],

                    'marketing_info' => array(
                        'user_id' => $arr['marketing_id'],
                        'name' => $marketing_info['realname'] . '',
                        //'mobile'  => $marketing_info['mobile'] . '',
                    ),

                    'purchase_info' => array(
                        'number' => $purchase_num,
                        'money' => $purchase_money,
                    ),
                ),
                'store' => (object)$store_info

            );
        }

        $result = $ret;

        if ($v > 1.6) {
            $result = array(
                'data_list' => $ret,

                'data_list_count' => count($ret),
                'data_list_c' => count($user_store_arr['data']),

                'fail_ids' => $fail_arr,
                'pages' => $user_store_arr['page'],

                // 未调价
                'unset_price_store_num' => (string)count($user_store_ids),
                // 未绑定设备，未调价
                'unset_device_store_num' => (string)count($unset_device_store_ids)
            );
        }

        if (!count($ret)) {
            api_helper::return_data(1, '暂无门店信息');
        } else {
            api_helper::return_data(0, 'success', $result);
        }
    }

    /**
     * 获取 合伙人的所有店铺信息(总店和第一分店)
     */
    public function list_agency_user_stores2() {
        // 验证接口
        $page_size = Request::getParam('page_size', '0');
        $page = Request::getParam('page_no', 1);
        $status = Request::getParam('status', '-1');
        // 只查连锁店
        $only_chain_shop = Request::getParam('only_chain_shop', '0');
        $type = Request::getParam('type', '0');
        $marketing_id = Request::getParam('marketing_id', 0);
        $dealer_id = Request::getParam('dealer_id', 0);
        $is_unset = Request::getParam('unset', 0);

        $check_params = array(
            'page_size' => $page_size,
            'page' => $page,
            'status' => $status,
            'only_chain_shop' => $only_chain_shop,
            'type' => $type
        );

        // 兼容老接口
        if ($page_size == 0) {
            $page_size = 100;
        } else {
            $page_size = 10;
        }

        $api_stat_id = api_helper::check_sign($check_params, 1);
        api_helper::get_user_info();

        if ($this->user_info['role_type'] == user_config::AGENCY) {
            //如果是合伙人
            $uid = $this->user_id;
            $filter['agency_id'] = $this->user_id;
            if ($marketing_id) {
                $filter['marketing_id'] = $marketing_id;
            }
        } elseif ($this->user_info['role_type'] == user_config::MARKETING) {
            //如果是市场人员
            $uid = $this->user_id;
            $filter['marketing_id'] = $this->user_id;
            $filter['sign_user_id'] = $this->user_id;
        } else {
            api_helper::return_data(1, '身份不对');
        }

        if (isset(store_config::$type_arr[$type])) {
            $filter['type'] = $type;
        }
        if ($status != -1) {
            $filter['status'] = $status;
        } else {
            $fitler['status'] = array(1, 2, 3);
        }

        //         if ($status != -1) {
        //             $all_user_store_arr = _model('store')->getList($filter, 'GROUP BY `owner_id` ORDER BY `id` DESC');
        //         } else {
        //             $all_user_store_arr = _model('store')->getList($filter, 'GROUP BY `owner_id` ORDER BY `id` DESC');

        //         }
        $all_user_store_arr = get_data_list('store', $filter, 'GROUP BY `owner_id` ORDER BY `id` DESC', $page, $page_size);

        $user_store_arr = array();
        foreach ($all_user_store_arr as $key => $val) {
            $cur_user_info = _uri('user_store', array('user_id' => $val['owner_id']));
            if ($cur_user_info && $cur_user_info['user_id'] != $this->user_id && $cur_user_info['user_id'] != 0) {
                array_push($user_store_arr, $cur_user_info);
            }
        }

        //先查 总店信息
        $ret = array();
        foreach ($user_store_arr as $arr) {
            $owner_id = $arr['user_id'];
            $owner_info = user_helper::get_user_info($owner_id);
            if (empty($owner_info)) {
                continue;
            }

            $filter_store = array(
                'owner_id' => $owner_id,
                //                 'cover != ' => '',
                //                 'title != ' => ''
            );
            $store_num_filter = array();
            $store_num_filter['owner_id'] = $owner_id;

            if ($status != -1) {
                $filter_store['status'] = $status;
                $store_num_filter['status'] = $status;
            }

            $store = _model('store')->read($filter_store, 'limit 0,1');

            $store_info = array();
            if (!empty($store)) {
                $store_user = user_helper::get_user_info($store['user_id']);

                if (!$store_user) {
                    continue;
                }

                // 获取当前总店id下所有分店的所有积分
                $owner_score = stat_helper::get_user_store_score($store['owner_id']);
                // 计算店铺等级
                $level = score_record_helper::get_user_score_level($owner_score);


                $store_info = array(
                    'store_id' => $store['id'],
                    'title' => $store['title'],
                    'title_branch' => $store['title_branch'],
                    'cover' => store_helper::cover($store['cover']),
                    'address' => $store['address'],
                    'address_code' => $store['district_id'],
                    'longitude' => $store['longitude'],
                    'latitude' => $store['latitude'],
                    'score' => $owner_score,
                    'level' => $level,
                    'status' => $store['status'],

                    'name' => empty($store_user['realname']) ? $owner_info['realname'] : $store_user['realname'],
                    'mobile' => empty($store_user['mobile']) ? $owner_info['mobile'] : $store_user['mobile'],
                );
            } else {
                continue;
            }

            //合伙人信息
            $marketing_info = user_helper::get_user_info($arr['marketing_id']);

            if (!$marketing_info) {
                continue;
            }

            //总进货信息
            // $purchase_info = stat_helper::get_owner_all_purchase($arr['user_id']);
            $purchase_num = _widget('stat.api_stat')->get_owner_sale_data($arr['user_id'], 'curr_month', 'box');
            $purchase_money = _widget('stat.api_stat')->get_owner_sale_data($arr['user_id'], 'curr_month', 'price');
            $purchase_money = num_keep_two_round($purchase_money);


            if ($this->user_info['role_type'] == user_config::MARKETING) {
                $store_num_filter['sign_user_id'] = $this->user_id;
            }
            // 同价门店数量
            $arr['store_num'] = _model('store')->getTotal($store_num_filter);
            // 获取撤店数量
            $stop_store_num = store_helper::get_store_num($owner_id, store_config::STATUS_STOP);

            $ret[] = array(
                'user_store' => array(
                    // 'store_id'       => $store_info['store_id'],
                    'owner_id' => $arr['user_id'],
                    'agency_id' => $arr['agency_id'],
                    'store_num' => $arr['store_num'],
                    'stop_store_num' => $stop_store_num,
                    'machine_num' => $arr['machine_num'],
                    'device_num' => device_helper::get_bind_device_count_by_owner_id($owner_id),
                    'add_time' => $arr['add_time'],


                    'name' => $owner_info['realname'],
                    'mobile' => $owner_info['mobile'],

                    'marketing_info' => array(
                        'user_id' => $arr['marketing_id'],
                        'name' => $marketing_info['realname'] . '',
                        'mobile' => $marketing_info['mobile'] . '',
                    ),

                    'purchase_info' => array(
                        'number' => $purchase_num,
                        'money' => $purchase_money,
                    ),
                ),
                'store' => (object)$store_info,
                // 未调价
                'unset_price_store_num' => (string)dealers_helper::get_unset_price_store_list($this->user_id),
                // 未绑定设备，未调价
                'unset_device_store_num' => (string)1
            );
        }

        api_helper::return_data(0, 'success', $ret);

    }


    /**
     * 获取 总店老板的所有分店信息
     */
    public function list_agency_owner_stores() {
        // 验证接口
        $pre_num = 100;
        $owner_id = Request::getParam('owner_id', 0);
        $status = Request::getParam('status', -1);
        $page_no = Request::getParam('page', 0);

        $check_params = array(
            'owner_id' => $owner_id,
            'status' => $status
        );

        $api_stat_id = api_helper::check_sign($check_params, 1);
        api_helper::get_user_info();

        $filter = array(
            'owner_id' => $owner_id,
            'status !=' => 0,
        );

        if ($this->user_info['role_type'] == user_config::STORE_OWNER) {
            //如果是总店店长身份
            $uid = $this->user_id;
            $filter['owner_id'] = $this->user_id;
        } elseif ($this->user_info['role_type'] == user_config::AGENCY) {
            //如果是合伙人身份
            $uid = $this->user_id;
            $filter['agency_id'] = $this->user_id;
        } elseif ($this->user_info['role_type'] == user_config::MARKETING) {
            //如果是市场人员身份
            $uid = $this->user_id;
            $filter['marketing_id'] = $this->user_id;
            $filter['sign_user_id'] = $this->user_id;
        } else {
            api_helper::return_data(1, '身份不对');
        }

        $user_store = store_helper::get_user_store($owner_id);
        if (empty($user_store)) {
            api_helper::return_data(1, '店铺不存在');
        }

        if ($status != -1) {
            $filter['status'] = $status;
        } else {
            $filter['status != '] = 0;
        }

        $list = get_data_list('store', $filter, 'ORDER BY `id` ASC', 1, $pre_num);
        $stores = array();
        foreach ($list as $store) {

            $user = user_helper::get_user_info($store['user_id']);
            if (!$user) {
                continue;
            }

            $type_str = '';
            if (isset(store_config::$type_arr[$store['type']])) {
                $type_str = store_config::$type_arr[$store['type']];
            }

            // 根据用户的积分数计算店铺等级
            $level = 1;
            $level = score_record_helper::get_user_score_level($user['score']);

            $stores[] = array(
                'store_id' => $store['id'],
                'type' => $store['type'],
                'store_order' => $store['store_order'],
                'type_str' => '',
                'title' => $store['title'],
                'title_branch' => $store['title_branch'],
                'cover' => store_helper::cover($store['cover']),
                'address' => $store['address'],
                'address_code' => $store['district_id'],
                'longitude' => $store['longitude'],
                'latitude' => $store['latitude'],
                'score' => $user['score'],
                'level' => $level,
                'name' => $user['realname'],
                'mobile' => $user['mobile'],
                'status' => $store['status'],
                'device_num' => device_helper::get_bind_device_type_count_by_store_id($store['id'])
            );

        }

        $ret = array(
            'user_store' => array(
                'owner_id' => $user_store['user_id'],
                'agency_id' => $user_store['agency_id'],
                'store_num' => $user_store['store_num'],
                'machine_num' => $user_store['machine_num'],
                'device_num' => device_helper::get_bind_device_count_by_owner_id($user_store['user_id']),
                'add_time' => $user_store['add_time'],
            ),
            'stores' => $stores

        );
        api_helper::return_data(0, 'success', $ret);
    }

    // 获取合伙人店铺信息
    public function get_agency_store_info() {
        // 验证接口
        $api_stat_id = api_helper::check_sign(array(), 1);

        if (!$this->user_id) {
            api_helper::return_data(1, '该用户不存在或已禁用');
        }

        $filter = array(
            'user_id' => $this->user_id,
            'status' => 1
        );
        $store_info = _model('store')->read($filter);
        if (!$store_info) {
            api_helper::return_data(1, '合伙人商铺信息有误');
        }
        api_helper::return_data(0, 'success', $store_info, $api_stat_id);
    }

    /**
     * 绑定店铺和设备
     */
    public function bind_store_device() {

        // 店铺id
        $store_id = Request::Post('store_id', 0);
        // 设备编号
        $device_no = Request::Post('device_no', '');
        // 状态
        $status = Request::Post('status', 2);
        // 解绑理由
        $unbind_reason = Request::Post('unbind_reason', '');
        // 附加信息
        $extra = Request::Post('extra', '');

        $check_params = array(
            'store_id' => $store_id,
            'device_no' => $device_no,
            'status' => $status,
            'unbind_reason' => $unbind_reason,
            'extra' => $extra
        );
        api_helper::check_sign($check_params, 1);

        $res = device_helper::bind_store_device($store_id, $device_no, $status, $unbind_reason, $extra);

        if ($res['flag'] == 'success') {
            api_helper::return_data(0, 'success');
        } else {
            api_helper::return_data(1, $res['info']);
        }

    }

    /**
     * 获取总店下分店的设备情况
     */
    public function get_store_device_num_list() {

        $store_id = Request::post('store_id', 0);

        $check_params = array(
            'store_id' => $store_id
        );
        api_helper::check_sign($check_params, 1);

        $store_list = store_helper::get_store_device_num_list($store_id);
        if ($store_list) {
            api_helper::return_data(0, 'success', $store_list);
        } else {
            api_helper::return_data(1, '暂无分店设备记录');
        }
    }

    /**
     * 获取单家门店下的设备绑定情况
     */
    public function get_store_device() {
        $store_id = Request::post('store_id', 0);
        $check_params = array(
            'store_id' => $store_id
        );
        api_helper::check_sign($check_params, 1);

        $store_info = store_helper::get_store_info($store_id);
        if (!$store_info) {
            api_helper::return_data(1, '这家店铺不存在');
        }

        $list = store_helper::get_store_device_list($store_id);
        if ($list) {
            api_helper::return_data(0, 'success', $list);
        } else {
            api_helper::return_data(1, '暂无设备信息');
        }

    }

    /**
     * 获取门店设备绑定概况信息(新)
     */
    public function get_store_device_info_list() {
        $store_id = Request::post('store_id', 0);
        $check_params = array(
            'store_id' => $store_id
        );
        api_helper::check_sign($check_params, 1);

        $store_info = store_helper::get_store_info($store_id);
        if (!$store_info) {
            api_helper::return_data(1, '这家店铺不存在');
        }

        $list = device_helper::get_store_device_list_by_store_id($store_id);

        api_helper::return_data(0, 'success', $list);
    }


    /**
     * 获取门店下的某一个分类下的设备列表(新)
     */
    public function get_store_device_list() {
        $store_id = Request::post('store_id', 0);
        $res_name = Request::Post('res_name', '');

        $check_params = array(
            'store_id' => $store_id,
            'res_name' => $res_name,
        );
        api_helper::check_sign($check_params, 1);

        $store_info = store_helper::get_store_info($store_id);
        if (!$store_info) {
            api_helper::return_data(1, '这家店铺不存在');
        }

        $RES = device_config::res_index();

        if (!array_key_exists($res_name, $RES)) {
            api_helper::return_data(1, '设备类型不存在');
        }

        $list = device_helper::get_store_typed_device_by_store_id($store_id, $res_name);

        api_helper::return_data(0, 'success', $list);
    }

    public function get_store_and_user_info() {
        $store_id = an_request('store_id');

        if (!$store_id) {
            api_helper::return_data(1, '请选择一个店铺');
        }

        $check_params = array(
            'store_id' => $store_id,
        );
        $api_stat_id = api_helper::check_sign($check_params, 1);

        $store = store_helper::get_store_info($store_id);
        $store_user = user_helper::get_user_info($store['user_id']);
        $agency_user_info = user_helper::get_user_info($store['agency_id']);

        $province_info = area_helper::get_area_info($store['province_id']);
        $city_info = area_helper::get_area_info($store['city_id']);
        $district_info = area_helper::get_area_info($store['district_id']);

        $data = array(
            'cover' => store_helper::cover($store['cover']),
            'title' => $store['title'],
            'title_branch' => $store['title_branch'],
            'realname' => $store_user['realname'],
            'mobile' => $store_user['mobile'],
            'weixin' => $store_user['weixin'],
            'type' => $store['type'],
            'type_name' => store_config::$type_arr[$store['type']],
            'is_allow_credit' => $store['is_allow_credit'],
            'province_code' => $store['province_id'],
            'city_code' => $store['city_id'],
            'district_code' => $store['district_id'],
            'province' => $province_info ? $province_info['area'] : '',
            'city' => $city_info ? $city_info['area'] : '',
            'district' => $district_info ? $district_info['area'] : '',
            'address' => $store['address'],
            'status' => $store['status'],
            'bbcoin' => format_money($store_user['bbcoin']),
            'user_money' => format_money($store_user['user_money']),
            'store_score' => (string)$store_user['score'],
            'device_num' => store_helper::is_store_bind_device($store_id),
            'agency_realname' => $agency_user_info['realname'],
            'agency_mobile' => $agency_user_info['mobile'],
        );

        api_helper::return_data(0, 'success', $data, $api_stat_id);
    }

    public function update_store_and_user_info() {
        $store_id= an_request('store_id');
        $realname = an_request('realname');
        $mobile = an_request('mobile');
        $weixin = an_request('weixin');
        $province_id = an_request('province_id');
        $city_id = an_request('city_id');
        $district_id = an_request('district_id');
        $address = an_request('address');

        $user_data = array(
            'realname' => $realname,
            'mobile' => $mobile,
            'weixin' => $weixin,
        );
        $store_data = array(
            'province_id' => $province_id,
            'city_id' => $city_id,
            'district_id' => $district_id,
            'address' => $address,
        );
        $check_params = array(
            'store_id' => $store_id
        );
        $check_params = array_merge($check_params, $user_data, $store_data);
        $this->validation_update_store_and_user_info($check_params);

        $api_stat_id = api_helper::check_sign($check_params, 1);

        $store = store_helper::get_store($store_id);
        if ($store['user_id'] != $this->user_id && $store['owner_id'] != $this->user_id) {
            api_helper::return_data(1, '你无权操作');
        }

        _model('user')->update($store['user_id'], $user_data);
        _model('store')->update($store_id, $store_data);

        api_helper::return_data(0, 'success', array(), $api_stat_id);
    }

    private function validation_update_store_and_user_info($params) {
        if (!$params['store_id']) {
            api_helper::return_data(1, '请选择一个店铺');
        }
        if (!$params['realname']) {
            api_helper::return_data(1, '姓名必填');
        }
        if (!$params['mobile']) {
            api_helper::return_data(1, '手机号必填');
        }
        if (!preg_match('/^1[3|4|5|7|8]\d{9}$/', $params['mobile'])) {
            api_helper::return_data(1, '手机号格式不正确');
        }
        if (!$params['province_id']) {
            api_helper::return_data(1, '省份必填');
        }
        if (!$params['city_id']) {
            api_helper::return_data(1, '城市必填');
        }
        if (!$params['district_id']) {
            api_helper::return_data(1, '地区必填');
        }
        if (!$params['address']) {
            api_helper::return_data(1, '详情地址必填');
        }
    }
}
